package com.pixel.game.entity;

import com.badlogic.gdx.math.Vector2;

import javax.swing.text.Position;

public class Unit {
    private float attackPower;
    private float health;
    private Vector2 position;

    private Vector2 targetPosition;
    private float attackCooldown; // 冷却时间
    private long lastAttackTime; // 上次攻击时间



    public Unit(Vector2 position, float attackPower, float health, float attackCooldown) {
        this.attackPower = attackPower;
        this.health = health;
        this.position = position;
        this.attackCooldown = attackCooldown;
        this.lastAttackTime = 0; // 初始设置为0，表示可立即攻击
    }

    public boolean canAttack(long currentTime) {
        return currentTime - lastAttackTime >= attackCooldown;
    }

    public void attack(Unit target, long currentTime) {
        if (canAttack(currentTime)) {
            target.takeDamage(attackPower);
            lastAttackTime = currentTime; // 更新上次攻击时间
        }
    }

    public void takeDamage(float damage) {
        health -= damage;
    }

    public boolean isAlive() {
        return health > 0;
    }

    public Vector2 getPosition() {
        return position;
    }

    public float getAttackPower() {
        return attackPower;
    }

    public void setAttackPower(float attackPower) {
        this.attackPower = attackPower;
    }

    public float getHealth() {
        return health;
    }

    public void setHealth(float health) {
        this.health = health;
    }

    public void setPosition(Vector2 position) {
        this.position = position;
    }

    public float getAttackCooldown() {
        return attackCooldown;
    }

    public void setAttackCooldown(float attackCooldown) {
        this.attackCooldown = attackCooldown;
    }

    public long getLastAttackTime() {
        return lastAttackTime;
    }

    public void setLastAttackTime(long lastAttackTime) {
        this.lastAttackTime = lastAttackTime;
    }

    public Vector2 getTargetPosition() {
        return targetPosition;
    }

    public void setTargetPosition(Vector2 targetPosition) {
        this.targetPosition = targetPosition;
    }

}
